<?php defined('MINICAKE') or exit('can not access!');
/**
 * Model 
 * 
 * @uses Object
 * @package 
 * @version $id$
 * @copyright 
 * @author lovise <lovise.lu@gmail.com> 
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
class Model extends Object{
	/**
	 * useTable 
	 * 定义模型对应的表名,可不填,默认自动等于类名.若该模型不使用数据库.请赋值为false.
	 * @var mixed
	 * @access public
	 */
	var $useTable = null;
	
	/**
	 * _params 
	 * 进行数据库操作时用到的参数数组
	 * @var array
	 * @access protected
	 */
	var $_params = array(
		'conditions' => null, 'fields' => null, 'order' => null, 'group' => null,'limit' => null
	);
	
	/**
	 * db
	 * 数据库资源
	 * @var array
	 * @access protected
	 */
	private static $db = null;

	/**
	 * __construct 
	 * 
	 * @access protected
	 * @return void
	 */
	public function __construct(){
		parent::__construct();
		$name = get_class($this);
		$database = Configure::getItem('database');
		$charset = Configure::getItem('charset');
		if($this->useTable === null){
			$this->useTable = $name;
		}
		if($this->useTable !== false){
			if(self::$db === null){
				if($database === 'SAE'){
					self::$db = new SaeMySql();
				}else{
					self::$db = new MyDB();
				}
				self::$db->setCharset($charset);
			}
		}
	}
	
	/**
	 * find 
	 * 
	 * @param string $type 
	 * @param array $params 
	 * @access public
	 * @return void
	 */
	function find($type='first',$params=array()){
		$params = array_merge($this->_params,$params);
		if($type !== 'count'){
			if($params['fields'] === null){
				$sql = "SELECT * FROM $this->useTable";
			}else{
				if(is_array($params['fields'])){
					$fields = $params['fields'][0];
					foreach(array_slice($params['fields'],1) as $val){
						$fields .= ','.$val;
					}
				}else{
					$fields = $params['fields'];
				}
				$sql = "SELECT $fields FROM $this->useTable";
			}
		}else{
			if($params['fields'] === null){
				$sql = "SELECT COUNT(*) FROM $this->useTable";
			}else{
				if(is_array($params['fields'])){
					$fields = "COUNT({$params['fields'][0]})";
					foreach(array_slice($params['fields'],1) as $val){
						$fields .= ",COUNT($val)";
					}
				}else{
					$fields = "COUNT({$params['fields']})";
				}
				$sql = "SELECT $fields FROM $this->useTable";
			}
			
		}
		if($params['conditions'] !== null){
			$conditions = '';
			foreach($params['conditions'] as $key => $val){
				$conditions .= " $key = '$val' AND";
			}
			$conditions = substr($conditions,1,-3);
			$sql .= " WHERE $conditions";
		}
		if($params['order'] !== null){
			if(is_array($params['order'])){
				$order = $params['order'][0];
				foreach(array_slice($params['order'],1) as $val){
					$order .= ','.$val;
				}
			}else{
				$order = $params['order'];
			}
			$sql .= " ORDER BY $order";
		}
		if($params['group'] !== null){
			if(is_array($params['group'])){
				$order = $params['group'][0];
				foreach(array_slice($params['group'],1) as $val){
					$order .= ','.$val;
				}
			}else{
				$order = $params['gruop'];
			}
			$sql .= " GROUP BY $order";
			
		}
		if($params['limit'] !== null){
			$sql .= " LIMIT {$params['limit']}";
		}
		switch($type){
			case 'all':
				return self::$db->getData($sql);
				break;
			default:
				return self::$db->getLine($sql);
				break;
		}
	}

	/**
	 * query 
	 * 
	 * @param mixed $sql 
	 * @access public
	 * @return void
	 */
	function query($sql){
		return  self::$db->getDate($sql);
	}

	/**
	 * save 
	 * 
	 * @param array $data 
	 * @access public
	 * @return void
	 */
	function save($data = array()){
		$fields = '';
		$values = '';
		foreach($data as $key => $val){
			$fields .= $key.',';
			$values .= "'$val',";
		}
		$fields = substr($fields,0,-1);
		$values = substr($values,0,-1);

		$sql = "INSERT INTO $this->useTable ($fields) VALUES ($values)";
		return self::$db->runSql($sql);
	}
	/**
	 * update 
	 * 
	 * @param array $fields 
	 * @param array $conditions 
	 * @access public
	 * @return void
	 */
	function update($fields = array(),$conditions = array()){
		$set = '';
		$where = '';
		foreach($fields as $key => $val){
			$set .= "$key = '$val',";
		}
		foreach($conditions as $key => $val){
			$where .= "$key = '$val',";
		}
		$set = substr($set,0,-1);
		$where = substr($where,0,-1);
		$sql = "UPDATE $this->useTable SET $set WHERE $where";

		return self::$db->runSql($sql);
	}
	/**
	 * delete 
	 * 
	 * @param array $conditions 
	 * @access public
	 * @return void
	 */
	function delete($conditions = array()){
		$where = '';
		foreach($conditions as $key => $val){
			$where .= "$key = '$val',";
		}
		$where = substr($where,0,-1);
		$sql = "DELETE FROM $this->useTable WHERE $where";

		return self::$db->runSql($sql);
		
	}
	/**
	 * __destruct 
	 * 
	 * @access public
	 * @return void
	 */
	public function __destruct(){
		if(self::$db !== null)
		self::$db->closeDB();
	}
}
?>
